<?php

namespace App\HttpController\Wx\Oauth;


use App\Common\Sys;
use App\HttpController\ControllerBase;
use XWX\Common\H;

/**
 * http://php.x.siyoumi.com/wx/oauth_re?x=1&word=test
 * http://php.x.siyoumi.com/wx/oauth_re?x=1&word=test&dev=
 *
 * Class oauth_re
 * @package App\HttpController\wx\oauth
 */
class OauthRe extends ControllerBase
{
    public function index()
    {
        $word = $this->reqParam('word');
        $scope = $this->reqParam('scope');
        //调试用
        $dev = $this->reqParam('dev');


        if (H::funcStrIsNullOrEmpty($word))
        {
            return $this->funcWriteErr(-100, '缺少重要参数');
        }

        if (H::funcStrIsNullOrEmpty($scope))
        {
            $scope = 'snsapi_base';
        }


        //获取用户信息,0否，1是|word
        $state = "0|{$word}";
        if ($scope == 'snsapi_userinfo')
        {
            $state = "1|{$word}";
        }
        $state = urlencode($state);


        $url_callback = Sys::funcAppOauthRoot() . "wx/oauth_callback?x={$this->funcGetX()}";
        if (H::funcStrHasAnyText($dev))
        {
            //调试用
            $url_callback .= "&dev={$dev}";
        }

        $url_callback = urlencode($url_callback);

        $config = $this->funcGetX__config();
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config->aconfig_app_id}"
            . "&redirect_uri={$url_callback}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";

//        $this->commonR()->setData('url', $url);
//        $this->funcWriteCommonR();
        $this->response()->redirect($url);
    }
}